// 模拟机试输入情况
// 第一行输入：接下来要输入的总行数
// 第二行开始到接下来的n行，需要记入数组
// 使用await + 迭代器形式

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const iter = rl[Symbol.asyncIterator]();

async function main() {
    console.log('How many pieces of your pizza?');
    let answer = parseInt((await iter.next()).value);

    console.log(
        `You entered: ${answer} pieces, please enter each size of your pizza`
    );

    let pizza = [];
    for (let i = 0; i < answer; i++) {
        let size = parseInt((await iter.next()).value);
        pizza.push(size);
    }

    console.log(`Pizza complete: ${JSON.stringify(pizza)}`);

    rl.close(); // 关闭 readline 接口以结束输入流

    const count = new Count(pizza);

    console.log(`Pizza count`);
    console.log(count.getCount());
}

class Count {
    constructor(array) {
        this.count = 0;
        this.count = array.reduce((acc, cur) => acc + cur, 0);
    }

    getCount() {
        return this.count;
    }
}

main().catch((err) => {
    console.error('An error occurred:', err);
});
